﻿namespace LeetCodeProject._0001_1000._001_100._71_80._076;

public class Solution
{
    public IList<IList<int>> Combine(int n, int k)
    {
        if (n <= 0 || k < 0 || k > n)
            throw new ArgumentException();
        if (k == 0)
        {
            return new List<IList<int>>(0);
        }

        var res = new List<IList<int>>();
        var buffer = new int[k];
        Combine(1, n, buffer, 0, res);
        return res;
    }

    private void Combine(int j1, int jn, int[] buffer, int i, List<IList<int>> result)
    {
        if (i == buffer.Length)
        {
            result.Add((int[])buffer.Clone());
            return;
        }

        for (int j = j1; j <= jn; j++)
        {
            buffer[i] = j;
            Combine(j + 1, jn, buffer, i + 1, result);
        }
    }
}